home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gchess40.lha / gnuchess4.0p62 / src / genmoves.c < prev    next >
C/C++ Source or Header  |  1993-06-22  |  9KB  |  348 lines

  1. /*
  2.  * genmoves.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24. short *TrP;
  25.  
  26. #define Link(from,to,flag,s) \
  27. {\
  28.    node->f = from; node->t = to;\
  29.      node->reply = 0;\
  30.        node->flags = flag;\
  31.      node->score = s;\
  32.        ++node;\
  33.          (*TrP)++;\
  34.          }
  35.  
  36. inline void
  37. LinkMove (short int ply, short int f,
  38.       register short int t,
  39.       short int flag,
  40.       short int xside)
  41.  
  42. /*
  43.  * Add a move to the tree.  Assign a bonus to order the moves as follows: 1.
  44.  * Principle variation 2. Capture of last moved piece 3. Other captures
  45.  * (major pieces first) 4. Killer moves 5.
  46.  */
  47.  
  48. {
  49.   register short s = 0;
  50. #if defined HISTORY
  51.   register short z;
  52. #endif
  53.   register unsigned short mv;
  54.   register struct leaf *node;
  55.  
  56.   node = &Tree[*TrP];
  57.   mv = (f << 8) | t;
  58. #ifdef KILLT
  59.   s += killt[mv | sidebit];
  60. #endif
  61. #ifdef HISTORY
  62.   z = mv;
  63.   if (xside == white) z |= 0x4000;
  64.   s += history[z];
  65. #endif
  66.   if (color[t] != neutral)
  67.     {
  68.       /* TOsquare is the square the last piece moved moved to */
  69.       s +=  value[board[t]] - board[f] + ((t == TOsquare) ? 500 : 0);
  70.     }
  71.   if (board[f] == pawn)
  72.     if (row (t) == 0 || row (t) == 7)
  73.       {
  74.     flag |= promote;
  75.     s += 800;
  76. #if !defined OLDXBOARD  && !defined GNU3 && !defined CHESSTOOL
  77.     Link (f, t, flag | queen, s - 20000);
  78.     s -= 200;
  79.     Link (f, t, flag | knight, s - 20000);
  80.     s -= 50;
  81.     Link (f, t, flag | rook, s - 20000);
  82.     flag |= bishop;
  83.     s -= 50;
  84. #else
  85.     flag |= queen;
  86. #endif
  87.       }
  88.     else if (row (t) == 1 || row (t) == 6)
  89.       {
  90.     flag |= pwnthrt;
  91.     s += 600;
  92.       }
  93.     else if ((row(t) == ((color[f] == white)?5:2)) && (ply > MINDEPTH) && (ply < Sdepth+3))
  94.       {
  95.     if ((mtl[white] - pmtl[white] + mtl[black] - pmtl[black]) < PTVALUE)
  96.       {
  97.         flag |= pwnthrt;
  98.         s += 400;
  99.       }
  100.       }
  101.   Link (f, t, flag, s - 20000);
  102. }
  103.  
  104. inline
  105. void
  106. GenMoves (register short int ply, register short int sq, short int side, short int xside)
  107.  
  108. /*
  109.  * Generate moves for a piece. The moves are taken from the precalulated
  110.  * array nextpos/nextdir. If the board is free, next move is choosen from
  111.  * nextpos else from nextdir.
  112.  */
  113.  
  114. {
  115.   register short u, piece;
  116.   register unsigned char *ppos, *pdir;
  117.  
  118.   TrP = &TrPnt[ply + 1];
  119.   piece = board[sq];
  120.   ppos = nextpos[ptype[side][piece]][sq];
  121.   pdir = nextdir[ptype[side][piece]][sq];
  122.   if (piece == pawn)
  123.     {
  124.       u = ppos[sq];        /* follow no captures thread */
  125.       if (color[u] == neutral)
  126.     {
  127.       LinkMove (ply, sq, u, 0, xside);
  128.       u = ppos[u];
  129.       if (color[u] == neutral)
  130.         LinkMove (ply, sq, u, 0, xside);
  131.     }
  132.       u = pdir[sq];        /* follow captures thread */
  133.       if (color[u] == xside && board[u] != king)
  134.     LinkMove (ply, sq, u, capture, xside);
  135.       u = pdir[u];
  136.       if (color[u] == xside && board[u] != king)
  137.     LinkMove (ply, sq, u, capture, xside);
  138.     }
  139.   else
  140.     {
  141.       u = ppos[sq];
  142.       do
  143.     {
  144.       if (color[u] == neutral)
  145.         {
  146.           LinkMove (ply, sq, u, 0, xside);
  147.           u = ppos[u];
  148.         }
  149.       else
  150.         {
  151.           if (color[u] == xside && board[u] != king)
  152.         LinkMove (ply, sq, u, capture, xside);
  153.           u = pdir[u];
  154.         }
  155.       } while (u != sq);
  156.     }
  157. }
  158.  
  159. void
  160. MoveList (short int side, register short int ply)
  161.  
  162. /*
  163.  * Fill the array Tree[] with all available moves for side to play. Array
  164.  * TrPnt[ply] contains the index into Tree[] of the first move at a ply.
  165.  */
  166.  
  167. {
  168.   register short i, xside, f;
  169.  
  170.   xside = side ^ 1;
  171.   TrP = &TrPnt[ply + 1];
  172.   *TrP = TrPnt[ply];
  173.   if (!PV)
  174.     Swag0 = killr0[ply];
  175.    else Swag0 = PV;
  176.   Swag1 = killr1[ply];
  177.   Swag2 = killr2[ply];
  178.   Swag3 = killr3[ply];
  179.   if (ply > 2)
  180.     Swag4 = killr1[ply - 2]; else Swag4 = 0;
  181. #ifdef KILLT
  182.   sidebit = ((side == white) ? 0 : 0x80);
  183.   killt[SwagHt | sidebit] += 5000;
  184.   killt[Swag0 | sidebit] += 2000;
  185.   killt[Swag1 | sidebit] += 60;
  186.   killt[Swag2 | sidebit] += 50;
  187.   killt[Swag3 | sidebit] += 40;
  188.   killt[Swag4 | sidebit] += 30;
  189. #endif
  190. #ifdef HISTORY
  191.   i = (side == black)?0x4000:0;
  192.   history[SwagHt | i] += 5000;
  193.   history[Swag0 | i] += 2000;
  194.   history[Swag1 | i] += 60;
  195.   history[Swag2 | i] += 50;
  196.   history[Swag3 | i] += 40;
  197.   history[Swag4 | i] += 30;
  198. #endif
  199.   for (i = PieceCnt[side]; i >= 0; i--)
  200.     GenMoves (ply, PieceList[side][i], side, xside);
  201.   if (!castld[side])
  202.     {
  203.       f = PieceList[side][0];
  204.       if (castle (side, f, f + 2, 0))
  205.     {
  206.       LinkMove (ply, f, f + 2, cstlmask, xside);
  207.     }
  208.       if (castle (side, f, f - 2, 0))
  209.     {
  210.       LinkMove (ply, f, f - 2, cstlmask, xside);
  211.     }
  212.     }
  213.   if (epsquare > 0)
  214.     {
  215.       f = epmove1[epsquare];
  216.       if (color[f] == side && board[f] == pawn)
  217.     LinkMove (ply, f, epsquare, capture | epmask, xside);
  218.       f = epmove2[epsquare];
  219.       if (color[f] == side && board[f] == pawn)
  220.     LinkMove (ply, f, epsquare, capture | epmask, xside);
  221.     }
  222. #ifdef KILLT
  223.   killt[SwagHt | sidebit] -= 5000;
  224.   killt[Swag0 | sidebit] -= 2000;
  225.   killt[Swag1 | sidebit] -= 60;
  226.   killt[Swag2 | sidebit] -= 50;
  227.   killt[Swag3 | sidebit] -= 40;
  228.   killt[Swag4 | sidebit] -= 30;
  229. #endif
  230. #ifdef HISTORY
  231.  i = (side == black)?0x4000:0;
  232.   history[SwagHt | i] -= 5000;
  233.   history[Swag0 | i] -= 2000;
  234.   history[Swag1 | i] -= 60;
  235.   history[Swag2 | i] -= 50;
  236.   history[Swag3 | i] -= 40;
  237.   history[Swag4 | i] -= 30;
  238. #endif
  239.   SwagHt = 0;            /* SwagHt is only used once */
  240.   GenCnt += (TrPnt[ply+1] - TrPnt[ply]);
  241. }
  242.  
  243. void
  244. CaptureList (register short int side, short int ply)
  245.  
  246. /*
  247.  * Fill the array Tree[] with all available cature and promote moves for side
  248.  * to play. Array TrPnt[ply] contains the index into Tree[] of the first move
  249.  * at a ply.
  250.  */
  251.  
  252. {
  253.   register short u, sq, xside;
  254.   register struct leaf *node;
  255.   register unsigned char *ppos, *pdir;
  256.   short i, piece, *PL, r7;
  257.  
  258.   xside = side ^ 1;
  259.   TrP = &TrPnt[ply + 1];
  260.   *TrP = TrPnt[ply];
  261.   node = &Tree[*TrP];
  262.   r7 = rank7[side];
  263.   PL = PieceList[side];
  264. #ifdef KILLT
  265.   sidebit = ((side == white) ? 0 : 0x80);
  266.   killt[SwagHt | sidebit] += 5000;
  267.   killt[Swag0 | sidebit] += 2000;
  268.   killt[Swag1 | sidebit] += 60;
  269.   killt[Swag2 | sidebit] += 50;
  270.   killt[Swag3 | sidebit] += 40;
  271.   killt[Swag4 | sidebit] += 30;
  272. #endif
  273.  
  274.   for (i = 0; i <= PieceCnt[side]; i++)
  275.     {
  276.       sq = PL[i];
  277.       piece = board[sq];
  278.       if (sweep[piece])
  279.     {
  280.       ppos = nextpos[piece][sq];
  281.       pdir = nextdir[piece][sq];
  282.       u = ppos[sq];
  283.       do
  284.         {
  285.           if (color[u] == neutral)
  286.         u = ppos[u];
  287.           else
  288.         {
  289.           if (color[u] == xside)
  290.             Link (sq, u, capture, value[board[u]] + svalue[u] - piece);
  291.           u = pdir[u];
  292.         }
  293.       } while (u != sq);
  294.     }
  295.       else
  296.     {
  297.       pdir = nextdir[ptype[side][piece]][sq];
  298.       if (piece == pawn && row (sq) == r7)
  299.         {
  300.           u = pdir[sq];
  301.           if (color[u] == xside)
  302.         Link (sq, u, capture | promote | queen, valueQ);
  303.           u = pdir[u];
  304.           if (color[u] == xside)
  305.         {
  306.           Link (sq, u, capture | promote | queen, valueQ);
  307. #if !defined OLDXBOARD  && !defined GNU3 && !defined CHESSTOOL
  308.           Link (sq, u, capture | promote | knight, valueN);
  309.           Link (sq, u, capture | promote | rook, valueR);
  310.           Link (sq, u, capture | promote | bishop, valueB);
  311. #endif
  312.         }
  313.           ppos = nextpos[ptype[side][piece]][sq];
  314.           u = ppos[sq];    /* also generate non capture promote */
  315.           if (color[u] == neutral)
  316.         {
  317.           Link (sq, u, promote | queen, valueQ);
  318. #if !defined OLDXBOARD  && !defined GNU3 && !defined CHESSTOOL
  319.           Link (sq, u, promote | knight, valueN);
  320.           Link (sq, u, promote | rook, valueR);
  321.           Link (sq, u, promote | bishop, valueB);
  322. #endif
  323.         }
  324.         }
  325.       else
  326.         {
  327.           u = pdir[sq];
  328.           do
  329.         {
  330.           if (color[u] == xside)
  331.             Link (sq, u, capture, value[board[u]] + svalue[u] - piece);
  332.           u = pdir[u];
  333.           } while (u != sq);
  334.         }
  335.     }
  336.     }
  337. #ifdef KILLT
  338.   sidebit = ((side == white) ? 0 : 0x80);
  339.   killt[SwagHt | sidebit] -= 5000;
  340.   killt[Swag0 | sidebit] -= 2000;
  341.   killt[Swag1 | sidebit] -= 60;
  342.   killt[Swag2 | sidebit] -= 50;
  343.   killt[Swag3 | sidebit] -= 40;
  344.   killt[Swag4 | sidebit] -= 30;
  345. #endif
  346.   SwagHt = 0;            /* SwagHt is only used once */
  347. }
  348.